public class Solution1022 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int ans=0;

    public int sumRootToLeaf(TreeNode root) {
        dfs(root,0);
        return ans;
    }

    private void dfs(TreeNode root, int k) {
        if (root==null){
            return;
        }
        k=k+root.val;
        if (root.left==null && root.right==null){
            ans+=k;
            return;
        }
        dfs(root.left,k*2);
        dfs(root.right,k*2);
    }

    public static void main(String[] args) {
        System.out.println(new Solution1022().sumRootToLeaf(new TreeNode(1,new TreeNode(0,new TreeNode(0),new TreeNode(1)),new TreeNode(1,new TreeNode(0),new TreeNode(1)))));
    }
}
